<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="教师姓名" prop="name">
      <el-input v-model="dataForm.name" placeholder="教师姓名"></el-input>
    </el-form-item>
    <el-form-item label="教师状态" prop="status">
      <!-- <el-input v-model="dataForm.status" placeholder="教师状态（0 正常 1 离职）"></el-input> -->
      <el-select v-model="dataForm.status" @change="bindTeacherChange" placeholder="请选择">
        <el-option
          v-for="item in teacherOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="讲师头像" prop="avatar">
      <el-upload
        class="avatar-uploader"
        :action= uploadUrl
        :show-file-list="false"
        :on-success="handleAvatarSuccess">
        <img v-if="imageUrl" :src="imageUrl" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
      <div class="tips">图片尺寸建议上传400*500</div>
    </el-form-item>
    <el-form-item label="教师简介" prop="career">
      <el-input type="textarea" rows="4" v-model="dataForm.career" placeholder="简介"  maxlength="300"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getNowFormatDate } from '@/utils'
import axios from 'axios'
import Vue from 'vue'
  export default {
    data () {
      return {
        
        uploadUrl: this.$http.adornUrl(`/sys/oss/upload?token=${this.$cookie.get('token')}`),
        // uploadUrl: `${window.SITE_CONFIG.baseUrl}/sys/common/upload`,
        visible: false,
        // headers:{token: this.$cookie.get('token')},
        teacherOptions: [{
            value: 0,
            label: '正常'
          }, {
            value: 1,
            label: '离职'
          }
        ],
        teacherValue: '',
        imageUrl: '',
        dataForm: {
          id: 0,
          name: '',
          status: '',
          career: '',
          avatar: '',
          sort: '',
          isDeleted: '',
          gmtCreate: '',
          gmtModified: '',
          
        },
        dataRule: {
          name: [
            { required: true, message: '教师姓名不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: "教师状态不能为空", trigger: ["blur",'change'] }
          ],
          career: [
            { required: true, message: '简介不能为空', trigger: 'blur' }
          ],
          // avatar: [
          //   { required: true, message: '讲师头像不能为空', trigger: 'blur' }
          // ],
        }
      }
    },
    methods: {
      bindTeacherChange(e){
        console.log(e);
      },
      handleAvatarSuccess(res, file) {
        this.imageUrl = res.url;
      },
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/eduteacher/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.eduTeacher.name
                this.dataForm.status = data.eduTeacher.status
                this.dataForm.career = data.eduTeacher.career
                this.dataForm.avatar = data.eduTeacher.avatar
                this.dataForm.sort = data.eduTeacher.sort
                this.dataForm.isDeleted = data.eduTeacher.isDeleted
                this.dataForm.gmtCreate = data.eduTeacher.gmtCreate
                this.dataForm.gmtModified = data.eduTeacher.gmtModified
                this.gmtCreate = data.eduTeacher.gmtCreate
                this.imageUrl = data.eduTeacher.avatar
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/eduteacher/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'status': this.dataForm.status,
                'career': this.dataForm.career,
                'avatar': this.imageUrl,
                // 'sort': this.dataForm.sort,
                // 'isDeleted': this.dataForm.isDeleted,
                // 'gmtCreate': this.dataForm.id? this.gmtCreate : getNowFormatDate(),
                // 'gmtModified': getNowFormatDate()
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
<style scoped>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
    border: 1px dotted #ccc;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  .tips{
    font-size: 12px;
    color: #d01f28;
  }
</style>
